package com.happypets.backstage;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.happypets.model.PetBean;
import com.happypets.servicesImpl.PetServiceImpl;
import com.happypets.utils.UploadUtil;

/**
 * Servlet implementation class PetDeal
 */
public class PetDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PetDeal() {
        super();
        // TODO Auto-generated constructor stub
    }
    

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String flag = request.getParameter("flag");
		
		System.out.println(flag);
		
		int a = 0;
		
		switch(flag){
			case "1": a=addPet(request);   break;
			case "2": a=updatePet(request);  break;
			case "3": a=updatePetImg(request);  break;
		}
		
		System.out.println(a==0?"失败":"成功");
		
		response.sendRedirect("backstage/pages/pets.html");
		
		
		
	}
	
	
	public int addPet(HttpServletRequest req){
		
		Map<String,String> pi = UploadUtil.fileUpload(req);
		
		PetServiceImpl psi = new PetServiceImpl();
		
		PetBean pet = psi.mapToPetBean2(pi);
		
		
		
		return psi.addPet(pet);
	}
	
	
	public int updatePet(HttpServletRequest req){
		
		Map<String,String> pi = UploadUtil.fileUpload(req);
		
		PetServiceImpl psi = new PetServiceImpl();
		
		PetBean pet = psi.mapToPetBean2(pi);
		
		
		
		
		return psi.updatePetInfo(pet);
	}
	
	public int updatePetImg(HttpServletRequest req){
		
		Map<String,String> pi = UploadUtil.fileUpload(req);
		
		PetServiceImpl psi = new PetServiceImpl();
		
		PetBean pet = psi.mapToPetBean3(pi);
		
		
		
		
		return psi.updatePetImage(pet);
	}



}
